package net.bwie.realtime.jtp.dws.log.job;

import com.alibaba.fastjson.JSON;
import net.bwie.realtime.jtp.dws.log.bean.PageViewBean;
import net.bwie.realtime.jtp.dws.log.function.PageViewBeanMapProcessFunction;
import net.bwie.realtime.jtp.dws.log.function.PageViewWindowFunction;
import net.bwie.realtime.jtp.utils.DorisUtil;
import net.bwie.realtime.jtp.utils.KafkaUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.time.Duration;


public class JtpLogPageViewMinuteWindowDwsJob {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.enableCheckpointing(5000);

        DataStream<String> kafkaStream = KafkaUtil.consumerKafka(env, "dwd-traffic-page-log");

//        kafkaStream.print("dwd-traffic-page-log--->");

        DataStream<String> handleStream = handle(kafkaStream);

        handleStream.print("handleStream--->");

        DorisUtil.saveToDoris(handleStream,"jtp_realtime_report","dws_traffic_page_view_window_report");

        env.execute("JtpLogPageViewMinuteWindowDwsJob");
    }

    private static DataStream<String> handle(DataStream<String> kafkaStream) {

        KeyedStream<String, String> midStream = kafkaStream.keyBy(
                json ->
                    JSON.parseObject(json).getJSONObject("common").getString("mid")
        );

        DataStream<PageViewBean> beanStream = midStream.map(new PageViewBeanMapProcessFunction());

        DataStream<PageViewBean> timeStream = beanStream.assignTimestampsAndWatermarks(
                WatermarkStrategy
                        .<PageViewBean>forBoundedOutOfOrderness(Duration.ofSeconds(0))
                        .withTimestampAssigner(new SerializableTimestampAssigner<PageViewBean>() {
                            @Override
                            public long extractTimestamp(PageViewBean element, long recordTimestamp) {
                                return element.getTs();
                            }
                        })
        );

        KeyedStream<PageViewBean, String> keyedStream = timeStream.keyBy(
                bean -> bean.getBrand() + "," + bean.getChannel() + "," + bean.getProvince() + "," + bean.getIsNew()
        );

        WindowedStream<PageViewBean, String, TimeWindow> window = keyedStream.window(TumblingEventTimeWindows.of(Time.minutes(1)));


        DataStream<String> apply = window.apply(new PageViewWindowFunction());

        return apply;
    }
}
